/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#include "pch.h"
#include <db/db_field_instance_t.h>

namespace test_wrappers { namespace storage {

std::shared_ptr<field_instance> createFieldInstance(
    const std::shared_ptr<field_definition>& fieldDefinition,
    const field_id& fieldInstanceId)
{
    std::shared_ptr<field_instance> instance;

    switch (fieldDefinition->type())
    {
    case type_unknown:
        throw db_exception("cannot instantiate unknown type", e_db_instantiate_unknown_type);
        break;

    case type_int16:
        {
            std::shared_ptr<field_definition_t<unsigned short>> fieldDefinitionT =
                std::dynamic_pointer_cast<field_definition_t<unsigned short>>(fieldDefinition);

            instance = std::make_shared<field_instance_t<unsigned short>>(*fieldDefinitionT, fieldInstanceId);
        }
        break;
    case type_int32:
        {
            std::shared_ptr<field_definition_t<unsigned int>> fieldDefinitionT =
                std::dynamic_pointer_cast<field_definition_t<unsigned int>>(fieldDefinition);

            instance = std::make_shared<field_instance_t<unsigned int>>(*fieldDefinitionT, fieldInstanceId);
        }
        break;
    case type_int64:
        {
            std::shared_ptr<field_definition_t<unsigned __int64>> fieldDefinitionT =
                std::dynamic_pointer_cast<field_definition_t<unsigned __int64>>(fieldDefinition);

            instance = std::make_shared<field_instance_t<unsigned __int64>>(*fieldDefinitionT, fieldInstanceId);
        }
        break;
    case type_float32:
        {
            std::shared_ptr<field_definition_t<float>> fieldDefinitionT =
                std::dynamic_pointer_cast<field_definition_t<float>>(fieldDefinition);

            instance = std::make_shared<field_instance_t<float>>(*fieldDefinitionT, fieldInstanceId);
        }
        break;
    case type_string_long:
        {
            std::shared_ptr<string_field> fieldDefinitionT =
                std::dynamic_pointer_cast<string_field>(fieldDefinition);

            instance = std::make_shared<field_instance_t<std::wstring>>(*fieldDefinitionT, fieldInstanceId);
        }
        break;
    case type_binaryBlob:
        {
            std::shared_ptr<blob_field> fieldDefinitionT =
                std::dynamic_pointer_cast<blob_field>(fieldDefinition);

            instance = std::make_shared<field_instance_t<binary_blob>>(*fieldDefinitionT, fieldInstanceId);
        }
        break;
    default:
        throw db_exception("cannot instantiate unknown type", e_db_instantiate_unknown_type);
    }

    return instance;
}

}} // test_wrappers::storage
